package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.FilePermission;
import com.tongtech.system.service.IFilePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 文件权限信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/file/permission")
public class FilePermissionController extends BaseController
{
    @Autowired
    private IFilePermissionService filePermissionService;

    /**
     * 获取文件权限列表
     */
    @RequiresPermissions("file:info:list")
    @GetMapping("/list")
    public TableDataInfo list(FilePermission filePermission)
    {
        startPage();
        //获取当前登录人，只查询分享给当前登录人的文件
        String userName = SecurityUtils.getUsername();
        if(Objects.nonNull(filePermission)){
            filePermission.setUserName(userName);
        }
        List<FilePermission> list = filePermissionService.selectFilePermissionList(filePermission);
        return getDataTable(list);
    }


    /**
     * 新增文件权限
     */
    @RequiresPermissions("file:info:add")
    @Log(title = "文件权限管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody FilePermission filePermission)
    {
       if(Objects.nonNull(filePermission)){
           filePermission.setCreateBy(SecurityUtils.getUsername());
       }
        return success(filePermissionService.insertFilePermission(filePermission));
    }

    /**
     * 批量新增文件权限
     */
    @RequiresPermissions("file:info:add")
    @Log(title = "文件权限管理", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@Validated @RequestBody List<FilePermission> filePermissions)
    {
        return success(filePermissionService.insertFilePermissionBatch(filePermissions));
    }

    /**
     * 删除文件权限
     */
    @RequiresPermissions("file:info:add")
    @Log(title = "文件权限管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(filePermissionService.deleteById(id));
    }

    /**
     * 获取文件分享列表
     */
    @RequiresPermissions("file:info:list")
    @GetMapping("/sharedList")
    public TableDataInfo sharedList(FilePermission filePermission)
    {
        startPage();
        List<FilePermission> list = filePermissionService.selectSharedList(filePermission);
        return getDataTable(list);
    }
}
